#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/k8s/k8s-v1.28.4-rocky94-init.sh | /bin/bash
#
#
############################################################################
#
# 先使用901号脚本初始化好rocky94的机器
# 在rocky94的linux裸机上直接执行该脚本，就可以安装好k8s-1.28.0相关的 <容器运行时三大件> + <kube三剑客> + <crictl>
# 已经处理过脚本逻辑，可以重复多次运行
# 该安装方法没有抄自任何博客，百分百内容来自于1.28.x的k8s官方文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
#




#***********************
# 机器初始化
#***********************
#关闭swap
swapoff -a

#安装k8s必备工具
yum install -y conntrack-tools iptables ebtables socat

#加载br_netfilter模块
modprobe br_netfilter
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables

#开启rocky94的网络转发
echo 1 > /proc/sys/net/ipv4/ip_forward

# 定义要追加的内容
HOSTS_BLOCK=$(cat << 'EOF'
192.168.5.231 rocky9-k8s1
192.168.5.232 rocky9-k8s2
192.168.5.233 rocky9-k8s3
EOF
)

# 检查并追加到 /etc/hosts
if ! grep -q "192.168.5.231" /etc/hosts; then
    echo "$HOSTS_BLOCK" >> /etc/hosts
fi


#***********************
# 安装containerd
#***********************
mkdir -p ~/bao && cd ~/bao

#这个速度慢，但是省钱（最好的办法是内网新建一个file服务器，明天再搞）
#wget https://file.taycc.com/d/bao/k8s/v1.28.4/containerd-1.7.23-linux-amd64.tar.gz

#这个速度快，但是每个月都要支付下行流量包的费用
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/containerd-1.7.23-linux-amd64.tar.gz
#wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/containerd-1.7.23-linux-amd64.tar.gz
mkdir -p /opt/websuite/containerd
rm -rf /opt/websuite/containerd/*
tar zxvf ./containerd-1.7.23-linux-amd64.tar.gz -C /opt/websuite/containerd/
mkdir -p /etc/containerd
/opt/websuite/containerd/bin/containerd config default > /etc/containerd/config.toml
cat << \EOF > /etc/systemd/system/containerd.service

# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime  # 服务的描述，说明这是 containerd 容器运行时
Documentation=https://containerd.io        # 服务的文档链接，提供更多信息
After=network.target local-fs.target       # 该服务将在网络和本地文件系统准备好后启动

[Service]
ExecStartPre=-/sbin/modprobe overlay       # 启动服务前尝试加载 overlay 内核模块，失败不影响后续启动
ExecStart=/opt/websuite/containerd/bin/containerd  # 服务启动命令，启动 containerd 进程

Type=notify                               # 服务类型为 notify，表示服务会发送通知以指示其启动状态
Delegate=yes                              # 允许服务使用 cgroup 的委托特性
KillMode=process                          # 结束服务时以进程方式结束所有子进程
Restart=always                            # 服务崩溃后自动重启
RestartSec=5                              # 重启服务前等待 5 秒

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity                        # 允许的最大进程数，设置为无限制
LimitCORE=infinity                         # 允许的最大核心转储文件大小，设置为无限制

# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity                          # 任务的最大数量，设置为无限制
OOMScoreAdjust=-999                        # 调整 OOM（Out Of Memory）分数，优先级设置为最低

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload
systemctl enable containerd
systemctl restart containerd




#***********************
# 安装runc
#***********************
mkdir -p ~/bao && cd ~/bao
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/runc-1.1.13.amd64
install -o root -g root -m 755 runc-1.1.13.amd64 /usr/local/sbin/runc




#***********************
# 安装cni
#***********************
mkdir -p ~/bao && cd ~/bao
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/cni-plugins-linux-amd64-v1.5.1.tgz
mkdir -p /opt/cni/bin
rm -rf /opt/cni/bin/*
tar Cxzvf /opt/cni/bin ./cni-plugins-linux-amd64-v1.5.1.tgz




#***********************
# 安装kubeadm+kubelet
#***********************
mkdir -p ~/bao && cd ~/bao
wget "http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/crictl-v1.28.0-linux-amd64.tar.gz"
tar zxvf crictl-v1.28.0-linux-amd64.tar.gz -C /usr/local/bin/
chown root:root /usr/local/bin/crictl

#安装kubeadm和kubelet
RELEASE="v1.28.0"
ARCH="amd64"
cd /usr/local/bin
wget "http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/kubeadm-1.28.0"
mv kubeadm-1.28.0 kubeadm
wget "http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/kubelet-1.28.0"
mv kubelet-1.28.0 kubelet
chmod +x {kubeadm,kubelet}

cat << \EOF > /etc/systemd/system/kubelet.service
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=https://kubernetes.io/docs/
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/kubelet
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target

EOF

mkdir -p /etc/systemd/system/kubelet.service.d
cat << \EOF > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/sysconfig/kubelet
ExecStart=
ExecStart=/usr/local/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS

EOF

systemctl daemon-reload
systemctl enable --now kubelet




#***********************
# 安装kubectl
#***********************
mkdir -p ~/bao && cd ~/bao
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.4/kubectl-1.28.4
mv kubectl-1.28.4 kubectl
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client




cat << EOF

====================================
总共安装了七个东西：

1.容器运行时：
- containerd
- runc
- cni

2.kubeadm相关组件
- kubelet
- kubeadm
- kubectl
- crictl

3.相关命令：
    systemctl daemon-reload
    systemctl restart containerd
    systemctl restart kubelet
====================================

EOF




